package com.yanchangapp.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zxl.baselib.util.time.TimeUtil;
import com.zxl.baselib.util.ui.ResHelper;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MailType;
import com.yanchangapp.model.bean.MailItemBean;
import com.yanchangapp.model.bean.MailUserBean;

import java.util.List;

/**
 * 对于这种全选和取消全选的事件
 *
 * 考虑使用
 */
public class MailListAdapter extends BaseQuickAdapter<MailItemBean, BaseViewHolder> {
    private boolean isEdit;
    private MailListListener mMailListListener;
    private boolean selectedState;
    private boolean hasBound = false;

    public void setIsNowEdit(boolean isEdit){
        this.isEdit = isEdit;
        setCleanAll();
    }

    public MailListAdapter(int layoutResId, @Nullable List<MailItemBean> data,MailListListener mailListListener) {
        super(layoutResId, data);
        this.mMailListListener = mailListListener;
    }

    public void setCleanAll(){
        this.selectedState = false;
        notifyDataSetChanged();
    }

    public void setSelectedAll(){
        this.selectedState = true;
        notifyDataSetChanged();
    }

    public void actionSuccess(){
        this.selectedState = false;
    }

    @Override
    protected void convert(BaseViewHolder helper, MailItemBean item) {
        // 设置选择按钮是否可见...
        int nowPosition = helper.getAdapterPosition();
        if (isEdit){
          helper.getView(R.id.rlLookStyle).setVisibility(View.GONE);
          helper.getView(R.id.rlEditStyle).setVisibility(View.VISIBLE);
        }else {
          helper.getView(R.id.rlEditStyle).setVisibility(View.GONE);
          helper.getView(R.id.rlLookStyle).setVisibility(View.VISIBLE);
        }

        View ivIcon =  helper.getView(R.id.blueDot);
        // 设置已读未读的图标展示,紧接着设置颜色咯
        if (item.getMailbox() == MailType.SENT.getMailDetail().getMailBoxType() ||
                item.getMailbox() == MailType.DRAFT.getMailDetail().getMailBoxType()) {
            ivIcon.setVisibility(View.GONE);
            helper.getView(R.id.tvMailSender).setSelected(true);
            helper.getView(R.id.rlLookStyle).setSelected(true);
            helper.getView(R.id.tvMailTheme).setSelected(true);
        }else {
            ivIcon.setVisibility(View.VISIBLE);
            if (item.getSeenFlag() == AppConstant.MAIL_UN_READ){
                ivIcon.setVisibility(View.VISIBLE);
                helper.getView(R.id.tvMailSender).setSelected(false);
                helper.getView(R.id.rlLookStyle).setSelected(false);
                helper.getView(R.id.tvMailTheme).setSelected(false);
            } else {
                ivIcon.setVisibility(View.GONE);
                helper.getView(R.id.tvMailSender).setSelected(true);
                helper.getView(R.id.rlLookStyle).setSelected(true);
                helper.getView(R.id.tvMailTheme).setSelected(true);
            }
        }

        // 设置姓名
        helper.setText(R.id.tvMailSender,getSendName(item.getToUsers(),item.getMailbox(),item.getSenderNickname()));
        // 设置姓名的头标...
        helper.setText(R.id.tvTextSingleName,
                getSingleIconText(item.getToUsers(),item.getMailbox(),item.getSenderNickname()));
        // 设置主题
        helper.setText(R.id.tvMailTheme,item.getSubject());
        // 设置内容
        helper.setText(R.id.tvMailContent,item.getContent());
        // 设置时间
        helper.setText(R.id.tvMailTime, TimeUtil.getTimeDes(item.getMailTime()));

        // 设置checkbox的点击事件
        CheckBox checkBox = helper.getView(R.id.checkbox);
        // 设置选中的状态
        checkBox.setChecked(selectedState);
        // cb点击事件
        hasBound = true;
        checkBox.setOnCheckedChangeListener(new MyCheckChangeListener(nowPosition));
        hasBound = false;
    }

    private class MyCheckChangeListener implements CompoundButton.OnCheckedChangeListener {
        private int position;

        public MyCheckChangeListener(int position) {
            this.position = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mMailListListener != null && !hasBound){
                mMailListListener.onCheckChangedListener(position,isChecked);
            }
        }
    }

    private String getSendName(List<MailUserBean> mList ,int mailBox,String sendName){
        String targetName = "";
        if (mailBox == MailType.SENT.getMailDetail().getMailBoxType() ||
            mailBox == MailType.DRAFT.getMailDetail().getMailBoxType()  ){
            if (mList != null && mList.size() != 0){
                targetName = mList.get(0).getNickname();
                for (int i=1;i<mList.size();i++){
                    targetName = targetName + ","+mList.get(i).getNickname();
                }
                return targetName;
            }
            return "(无收件人)";
        }
        return sendName;
    }

    private String getSingleIconText(List<MailUserBean> mList , int mailBox, String sendName){
        String targetName;
        if (mailBox == MailType.SENT.getMailDetail().getMailBoxType() ||
                mailBox == MailType.DRAFT.getMailDetail().getMailBoxType()  ){
            if (mList != null && mList.size() != 0){
                targetName = mList.get(0).getNickname();
                if (TextUtils.isEmpty(targetName)){
                    return " ";
                }
                return targetName.substring(0,1);
            }
            return " ";
        }
        if (TextUtils.isEmpty(sendName)){
            return " ";
        }
        if (TextUtils.isEmpty(sendName)){
            return " ";
        }
        return  sendName.substring(0,1);
    }

    public interface MailListListener{
        void onCheckChangedListener(int position,boolean state);
    }

}
